<?php
include_once(PATH_SQL . 'product_sql.php');
include_once(PATH_MODEL . 'product_model.php');
include_once(PATH_VIEW_ADMIN . 'product_view.php');

$oModelProduct=new Model_Product();
$oViewProduct = new View_Product($oModelProduct);
$oModelProduct->setData('page_size',50);
$oModelProduct->setData('pagegroup_size',5);
$oModelProduct->setData('stylesheet',"paging");
$sAction=$_REQUEST['sAction'];
switch($sAction)
{
	case '':
		{
			unset($_SESSION['fromDate']);
			unset($_SESSION['toDate']);
			$oViewProduct->displayManagerIndex($oModelProduct);
			$oViewProduct->displayListProduct($oModelProduct);
			break;
		}
	case "export_csv":
		{ 
		$oViewProduct->display_csv($oModelProduct);	 
        $array_header= $oModelProduct->aLanguage['HEADER_CSV_FILE'];
		$csv='';
		folderDelete(PATH_ROOT."auctions");
		//mkdir(PATH_ROOT."auctions",777);
		//chown(PATH_ROOT."auctions", 'blali');
		$fp=fopen(PATH_ROOT.'auctions/auction.csv','w');
		foreach ($array_header as $header)
			{
			$header	=mb_convert_encoding($header,"SJIS","UTF-8");
			$csv .= $header.",";
			$arr_header[]=$header;
			}
		$csv= substr($csv,0,strlen($csv)-1);
		$csv.="\n";
		
		$aData= $oModelProduct->getListExportCsv();
if (is_array($aData))		
foreach($aData as $row)
	{	
	// get province
			$product_province_id=	$row['product_province_id']; 
			$getProvinceNamme = $oModelProduct->getOneProvince($product_province_id);
			$row['product_province_id'] = $getProvinceNamme;
	//product_who_bear_the_CsOfTr
			if (intval($row['product_who_bear_the_CsOfTr']==1)) {
				$row['product_who_bear_the_CsOfTr']		=$oModelProduct->aLanguage['DATA_SHOW']['Who_wins_the_auction'];
			} else $row['product_who_bear_the_CsOfTr']	=$oModelProduct->aLanguage['DATA_SHOW']['owner_auction'];
	//product_payment_of_goods_boa
			if (intval($row['product_payment_of_goods_boa']==1)) {
				$row['product_payment_of_goods_boa']	=$oModelProduct->aLanguage['DATA_SHOW']['payment_of_goods_before'];
			} else $row['product_payment_of_goods_boa']	=$oModelProduct->aLanguage['DATA_SHOW']['payment_of_goods_after'];
	//product_payment_method, payment htrough bank 
			if (intval($row['product_payment_method']==1)) {
				$row['product_payment_method']			=$oModelProduct->aLanguage['DATA_SHOW']['payment_throught_bank_1'];
				$arrayBankName = explode(">*",$row['product_bank']);
				$count_bank_name = count($arrayBankName);

				for($i=1;$i<=$count_bank_name;$i++){
						$item_bank = "product_bank_".$i;
						$row[$item_bank] =$arrayBankName[$i-1];
						}
				for($i=$count_bank_name+1;$i<=10;$i++)
						{
						$item_bank = "product_bank_".$i;
						$row[$item_bank] ='    ';
						}
					
				$csv_bank='';
				for($i=1;$i<=10;$i++)
						{
						$item_bank = "product_bank_".$i;
						$row[$item_bank] =	mb_convert_encoding($row[$item_bank],"SJIS","UTF-8");
						$csv_bank .= $row[$item_bank]." ,";
						}
					
			} else{ $csv_bank='';
				$row['product_payment_method']	=	$oModelProduct->aLanguage['DATA_SHOW']['payment_throught_bank_0'];
				for($i=1;$i<=10;$i++)
						{
						$item_bank = "product_bank_".$i;
						$row[$item_bank] =' ';
						$csv_bank .= $row[$item_bank].",";
						}
				}
	$csv_bank =substr($csv_bank,0,strlen($csv_bank)-1);
			
	//product_send_cash 
			if (intval($row['product_send_cash']==1)) {
				$row['product_send_cash']		=	$oModelProduct->aLanguage['DATA_SHOW']['product_send_cash_1'];
			} else $row['product_send_cash']	=	$oModelProduct->aLanguage['DATA_SHOW']['product_send_cash_0'];
	//product_POD 
			if (intval($row['product_POD']==1)) {
				$row['product_POD']		=	$oModelProduct->aLanguage['DATA_SHOW']['product_POD_1'];
			} else $row['product_POD']	=	$oModelProduct->aLanguage['DATA_SHOW']['product_POD_0'];		
	//product_credit_card
			if (intval($row['product_credit_card']==1)) {
				$row['product_credit_card']		=	$oModelProduct->aLanguage['DATA_SHOW']['product_credit_card_1'];
			} else $row['product_credit_card']	=	$oModelProduct->aLanguage['DATA_SHOW']['product_credit_card_0'];		

	//Other payment
if ($row['product_other_payment_method']!='') {
			
				$arrayOtherPM = explode(">*",$row['product_other_payment_method']);
				$count_Other = count($arrayOtherPM);

				for($i=1;$i<=$count_Other;$i++){
						$item_other = "product_other_payment_method_".$i;
						$row[$item_other] =$arrayOtherPM[$i-1];
						}
				for($i=$count_Other+1;$i<=10;$i++)
						{
						$item_other = "product_other_payment_method_".$i;
						$row[$item_other] =' ';
						}
					
			} else
//				$row['product_other_payment_method']	=	$oModelProduct->aLanguage['HEADER_CSV_FILE']['product_other_payment_method'];
				for($i=1;$i<=10;$i++)
						{
						$item_bank = "product_other_payment_method_".$i;
						$row[$item_bank] =' ';
						}

$csv_other_payment_method='';
for($i=1;$i<=10;$i++)
			{
			$item_other = "product_other_payment_method_".$i;
			$row[$item_other] =	mb_convert_encoding($row[$item_other],"SJIS","UTF-8");
			$csv_other_payment_method .= $row[$item_other]." ,";
			}	
					
$csv_other_payment_method =substr($csv_other_payment_method,0,strlen($csv_other_payment_method)-1);	

			$csv_other_trans='';
			$aDataOMoT = $oModelProduct->getOMoT($row['product_id']);
			$count_OtherMO = 0;
			
			if(is_array($aDataOMoT)){
			$count_OtherMO = count($aDataOMoT);
			foreach ($aDataOMoT as $dataOMoT)
				{
//					print_r($dataOMoT); die();
				$item_other_title =  mb_convert_encoding($dataOMoT['other_title_of_method'],"SJIS","UTF-8");
				$item_link_to_price_list =  mb_convert_encoding($dataOMoT['other_link_to_price_list'],"SJIS","UTF-8");
				$item_uniform_price =  mb_convert_encoding($dataOMoT['other_uniform_price'],"SJIS","UTF-8");
				$csv_other_trans	.= $item_other_title.' ,'.$item_link_to_price_list.' ,'.$item_uniform_price.' ,';
				}
				}
					
			for($i=$count_OtherMO+1;$i<=10;$i++)
						$csv_other_trans.= " , , ,";

$csv_other_trans =substr($csv_other_trans,0,strlen($csv_other_trans)-1);			
								
	//product_product_status
			if (intval($row['product_product_status'])==1) 
				$row['product_product_status'] = $oModelProduct->aLanguage['DATA_SHOW']['product_SecondHand'];
			elseif (intval($row['product_product_status'])==2) 
				$row['product_product_status'] = $oModelProduct->aLanguage['DATA_SHOW']['product_new'];
				else 
				$row['product_product_status'] = $oModelProduct->aLanguage['DATA_SHOW']['product_other'];

			if (intval($row['product_ability_to_return_product']==1)) {
				$row['product_ability_to_return_product']		=	$oModelProduct->aLanguage['DATA_SHOW']['product_ability_to_return_product_1'];
			} else $row['product_ability_to_return_product']	=	$oModelProduct->aLanguage['DATA_SHOW']['product_ability_to_return_product_0'];		
			

$row['product_explanation'] = ereg_replace("/\n\r|\r\n|\n|\r/", "", $row['product_explanation']);
$row['product_description'] = preg_replace("/\t/", "", $row['product_explanation']);

$row['product_size'] = ereg_replace("/\n\r|\r\n|\n|\r/", "", $row['product_size']);
$row['product_description'].='<br/>'.preg_replace("/\t/", "", $row['product_size']);

$row['product_rank'] = ereg_replace("/\n\r|\r\n|\n|\r/", "", $row['product_rank']);
$row['product_description'].= '<br/>'.preg_replace("/\t/", "", $row['product_rank']);

$row['product_note'] = ereg_replace("/\n\r|\r\n|\n|\r/", "", $row['product_note']);
$row['product_description'].='<br/>'. preg_replace("/\t/", "", $row['product_note']);

	if (intval($row['product_auto_extend']==1)) {
				$row['product_auto_extend']		=	$oModelProduct->aLanguage['DATA_SHOW']['product_auto_extend_1'];
			} else $row['product_auto_extend']	=	$oModelProduct->aLanguage['DATA_SHOW']['product_auto_extend_0'];		
	
			if (intval($row['product_end_soon']==1)) {
				$row['product_end_soon']		=	$oModelProduct->aLanguage['DATA_SHOW']['product_end_soon_1'];
			} else $row['product_end_soon']	=	$oModelProduct->aLanguage['DATA_SHOW']['product_end_soon_0'];		

	if (intval($row['product_have_to_negotiate_discount']==1)) 
	{
				$row['product_have_to_negotiate_discount']		=	$oModelProduct->aLanguage['DATA_SHOW']['product_have_to_negotiate_discount_1'];
			} else $row['product_have_to_negotiate_discount']	=	$oModelProduct->aLanguage['DATA_SHOW']['product_have_to_negotiate_discount_0'];		

	if (intval($row['product_title_bold_line']==1)) {
				$row['product_title_bold_line']		=	$oModelProduct->aLanguage['DATA_SHOW']['product_title_bold_line_1'];
			} else $row['product_title_bold_line']	=	$oModelProduct->aLanguage['DATA_SHOW']['product_title_bold_line_0'];	
	
	if (intval($row['product_store_hot_auction']==1)) {
				$row['product_store_hot_auction']		=	$oModelProduct->aLanguage['DATA_SHOW']['product_store_hot_auction_1'];
			} else $row['product_store_hot_auction']	=	$oModelProduct->aLanguage['DATA_SHOW']['product_store_hot_auction_0'];		
	
	if (intval($row['product_bakground_color']==1)) {
				$row['product_bakground_color']		=	$oModelProduct->aLanguage['DATA_SHOW']['product_bakground_color_1'];
			} else $row['product_bakground_color']	=	$oModelProduct->aLanguage['DATA_SHOW']['product_bakground_color_0'];						

	//if (intval($row['product_icon_highlight']==1)) {
//				$row['product_icon_highlight']		=	$oModelProduct->aLanguage['DATA_SHOW']['product_icon_highlight_1'];
//			} else $row['product_icon_highlight']	=	$oModelProduct->aLanguage['DATA_SHOW']['product_icon_highlight_0'];						
	
			if (intval($row['product_icon_gift']==1)) {
				$row['product_icon_gift']		=	$oModelProduct->aLanguage['DATA_SHOW']['product_icon_gift_1'];
			} else $row['product_icon_gift']	=	$oModelProduct->aLanguage['DATA_SHOW']['product_icon_gift_0'];						
	
	//if (intval($row['product_yahoo_point']==1)) {
//				$row['product_yahoo_point']		=	$oModelProduct->aLanguage['DATA_SHOW']['product_yahoo_point_1'];
//			} else $row['product_yahoo_point']	=	$oModelProduct->aLanguage['DATA_SHOW']['product_yahoo_point_0'];						
	

	if (intval($row['product_affiliate']==1)) {
				$row['product_remuneration_to_partner']		=	$row['product_remuneration_to_partner']	;
		} else $row['product_remuneration_to_partner']	=	'';
	if (intval($row['product_use_yahoo']==1)) {
				$row['product_use_yahoo']		=	$row['product_use_yahoo_1']	;
		} else $row['product_use_yahoo']	=	$row['product_use_yahoo_0']	;
	if (intval($row['product_transport_goods_abroad']==1)) {
			$row['product_transport_goods_abroad']		=	$oModelProduct->aLanguage['DATA_SHOW']['product_transport_goods_abroad_1'];
		} else{
			 $row['product_transport_goods_abroad']	=	$oModelProduct->aLanguage['DATA_SHOW']['product_transport_goods_abroad_0'];
			 //echo $row['product_transport_goods_abroad']; die();
			}
			
	/**
	 * display image
	 */
	 $arr_image=$oModelProduct->getImage($row['product_id']);
	 if(is_array($arr_image))
	 {
	 	$num=1;
	 	foreach($arr_image as $row_image)
	 	{
	 		$row['pictures_product_id_'.$num]= $row_image['pictures_file_name'];//"2009040800003.jpg";
	 		$row['pictures_comment_'.$num]=$row_image['pictures_comment'];
	 		$num++;
	 		$file = PATH_UPLOAD_PRODUCT.$row_image['pictures_file_name'];
			$newfile = PATH_ROOT.'auctions/'.$row_image['pictures_file_name'];
			copy($file,$newfile);  
	 	}
	 }
	 
	 	
				
	$row['product_auction_english_version']		=	"";// not exist in data base
	$row['product_description_for_english_version']	=	"";
	$row['product_title_of_english_version']	=	"";
		
		$arrayNameKey = array_keys($row);
		
	// convert data k aja	
	foreach ($arrayNameKey as  $filed)
	//print_r($row[$file]);
	//die();
		$row[$filed]=mb_convert_encoding($row[$filed],"SJIS","UTF-8");
		
		$csv .= $row['product_id'].",".$row['product_category_id'].",".$row['product_title'].",".$row['product_description'].",";
		$csv .= $row['product_description_on_mobile'].",".$row['product_prices_start'].",".$row['product_qty'].",".$row['product_time_auction'].",";		
		$csv .= $row['product_time_auction_end'].",".$row['product_province_id'].",".$row['product_shipping_area'].",".$row['product_who_bear_the_CsOfTr'].",";		
		$csv .= $row['product_payment_of_goods_boa'].",".$row['product_payment_method'].",".$csv_bank.",".$row['product_send_cash'].",";
		$csv .= $row['product_POD'].",".$row['product_credit_card'].",".$csv_other_payment_method.",".$row['product_product_status'].",";	
		$csv .= $row['product_product_status_comment'].",".$row['product_ability_to_return_product'].",".$row['product_ability_to_return_product_comment'].",";
		
		$csv .=$row['pictures_product_id_1'].",".$row['pictures_comment_1'].",".$row['pictures_product_id_2'].",";		
		$csv .= $row['pictures_comment_2'].",".$row['pictures_product_id_3'].",".$row['pictures_comment_3'].",";
				
		$csv .= $row['product_limited_assesment_of_the_bidder'].",".$row['product_auto_extend'].",".$row['product_end_soon'].",".$row['product_price_fastener'].",".$row['product_have_to_negotiate_discount'].",";
		$csv .=$row['product_auto_open_aution'].",".$row['product_price_wins_minimum'].",";		
		$csv .= $row['product_auction_notable'].",".$row['product_title_bold_line'].",".$row['product_bakground_color'].",".$row['product_store_hot_auction'].",";		
		$csv .= $row['product_icon_highlight'].",".$row['product_icon_gift'].",".$row['product_yahoo_point'].",".$row['product_remuneration_to_partner'].",";		
		$csv .= $row['product_size_after_packet'].",".$row['product_weight_after_packet'].",".$row['product_use_yahoo'].",";
		$csv .= $csv_other_trans.",".$row['product_transport_goods_abroad'].",";		
		$csv .= $row['product_auction_english_version'].",".$row['product_title_of_english_version'].",".$row['product_description_for_english_version'];
			
		$csv.="\n";
		$arr_data=null;
		$arr_data=explode(',',$csv);	
	}
				//$check_backup=$oModelProduct->backUpAfterExortCsv();
		ob_clean();
		fwrite($fp,$csv);
		fclose($fp);
		
				
	include_once("CreateZipFile.inc.php");
//	$directoryToZip="../auctions";  //This will zip all the file(s) in this present working directory
//	$outputDir="";  //Replace "/" //with the name of the desired output directory.
//	$zipName="test.zip"; 
	$createZipFile=new CreateZipFile; 
//	$createZipFile->zipDirectory($directoryToZip,$outputDir);
//	$fd=fopen(PATH_ROOT.'auctions'.$zipName, "w");
//	$out=fwrite($fd,$createZipFile->getZippedfile());
//	fclose($fd);
	$zipName="auctions.zip"; 
	@unlink(PATH_ROOT.$zipName);
	$files_to_zip= dirList(PATH_ROOT."auctions");
	$result = create_zip($files_to_zip,PATH_ROOT.$zipName);
	$createZipFile->forceDownload(PATH_ROOT.$zipName); 
	die();
	
	break;
		}
	case "update_size"://not use yet
		{
			$oViewProduct->displayAddNewForm($oModelProduct);
			$oViewProduct->ajaxGetCurrentAtr($oModelProduct,$_REQUEST['product_id']);
			die();
			break;
		}		
	case "AJAX_RELOAD_IMAGE":
		{
			$oViewProduct->displayAddNewForm($oModelProduct);
			$oViewProduct->imageOfThisP_Ajax($oModelProduct,$_REQUEST['product_id']);
			die();
			break;
		}		
	case "filterProduct": // ajax but had bug so submit normal
		{
			$oViewProduct->displayManagerIndex($oModelProduct);
			$oModelProduct->setData('fromDate',$_REQUEST['fromDate']);
			$oModelProduct->setData('toDate',$_REQUEST['toDate']);
			$oViewProduct->resultXML_filterProduct($oModelProduct);
			die();
			break;		
		}
	case "filterProduct_normal": // ajax but had bug so submit normal
		{
//			echo "4234234"; die();
			$oViewProduct->displayManagerIndex($oModelProduct);
			$oModelProduct->setData('fromDate',$_REQUEST['fromDate']);
			$oModelProduct->setData('toDate',$_REQUEST['toDate']);
			$oModelProduct->setData('category',$_REQUEST['category']);
			unset($_SESSION['fromDate']);
			unset($_SESSION['toDate']);
			unset($_SESSION['category']);
			$_SESSION['fromDate'] = $_REQUEST['fromDate'];
			$_SESSION['toDate'] = $_REQUEST['toDate'];
			$_SESSION['category'] = $_REQUEST['category'];
			$oViewProduct->filterProduct($oModelProduct);
			break;		
		}
	   case 'update_status':
        {
            $oModelProduct->unsetData('product_id');
            $oModelProduct->unsetData('product_yahoo_point');
            if (count($_POST['hotel_page_sort_yahoo'] > 0)) {
                foreach ($_POST['hotel_page_sort_yahoo'] as $sKey => $sValue) {
                    $oModelProduct->setData('product_id', $sKey);
//                    $oModelProduct->setData('product_yahoo_point', intval($_POST['product_yahoo_point'][$sKey]));
//                    $oModelProduct->setData('product_icon_highlight', intval($_POST['product_icon_highlight'][$sKey]));
                    $oModelProduct->setData('product_icon_gift', intval($_POST['product_icon_gift'][$sKey]));
                    $oModelProduct->setData('product_store_hot_auction', intval($_POST['product_store_hot_auction'][$sKey]));
                    $oModelProduct->setData('product_bakground_color', intval($_POST['product_bakground_color'][$sKey]));
                    $oModelProduct->setData('product_title_bold_line', intval($_POST['product_title_bold_line'][$sKey]));
                    $oModelProduct->setData('product_have_to_negotiate_discount', intval($_POST['product_have_to_negotiate_discount'][$sKey]));
                    $oModelProduct->updateProductStatus();
                }
            }
          if (isset($_SESSION['fromDate']) && isset($_SESSION['toDate'])){
          	redir('?sMod=product_index&sAction=filterProduct_normal&fromDate='.$_SESSION['fromDate'].'&toDate='.$_SESSION['toDate']);
          }
     		else   
     			redir('?sMod=product_index');
            break;
        }
	case "add_new_form":
		{
			$oModelProduct->setData("do_edit_or_add_new","do_add_new");
			$oViewProduct->displayAddNewForm($oModelProduct);
			$oViewProduct->viewListProvince($oModelProduct);
			$oViewProduct->showAttributeAddNew($oModelProduct);
			
			$rs=$oViewProduct->displayListTemplate($oModelProduct,'');
			$oViewProduct->displayTemplateDefault($oModelProduct,$rs);
			$oViewProduct->displayRank($oModelProduct,'');
			
			$oViewProduct->displayBlockProductBankInputDefault($oModelProduct);
			break;
		}
	case "do_add_new":
		{
			$_POST["product_date_added"] = date('Y-m-d H:i:s');
			$_POST["product_last_modified"] = date('Y-m-d H:i:s');
			$_POST["product_name"] = $_POST["product_title"];
		//	if (intval($_POST["product_description_normal"]) == 0) {
//				$_POST["product_description"]  = $_POST["product_description_html_tag"];
//				$oModelProduct->setData("product_description",$_POST["product_description"]);
//			}

			if($_POST['product_description_swich']==1)
			{
				$oModelProduct->setData('product_description_on_mobile',$_POST['product_description_on_mobile_unactive']);
			}
			
			
			$oModelProduct->setData("product_description",$_POST["product_description"]);
			$oModelProduct->setData("product_date_added",$_POST["product_date_added"]);
			$oModelProduct->setData("product_last_modified",$_POST["product_last_modified"]);
			$oModelProduct->setData("product_name",$_POST["product_title"]);
			$oModelProduct->setData("product_status",1);
			$oModelProduct->setData("product_description_normal",1);// tab normal is selected
			//$result_editAttribute=$oModelProduct->saveAttribute($_REQUEST);
			if($_POST['product_person']=='')
   			{
   				$oModelProduct->setData("product_set_limit_on_prices",'');
   			}
			
			/**
			 * BANK && OTHER MOETHOD
			 */
				$product_payment_method='';
				if(is_array($_POST['product_bank']))
				{
					foreach($_POST['product_bank'] as $arr_row)
					{
						if($arr_row!='')
						{	$product_payment_method=1;
							$arr_bank_temp[]=$arr_row;
						}
					}
					if(is_array($arr_bank_temp))
					{	
					$arr_bank=implode('>*',$arr_bank_temp);
					}
					else
					$arr_bank=null;
					$oModelProduct->setData('product_bank',$arr_bank);
				}
			if($product_payment_method==1 && $_POST['product_payment_method']==1)
			{
				
				$oModelProduct->setData('product_payment_method','1');	
			}
			else
			{
				
				$oModelProduct->setData('product_payment_method','0');
			}	
			//OTHER METHOD
			if(is_array($_POST['product_other_payment_method']))
				{
					foreach($_POST['product_other_payment_method'] as $arr_row1)
					{
						if($arr_row1!='')
						{	
							$arr_bank_temp1[]=$arr_row1;
						}
					}
					if(is_array($arr_bank_temp1))
					{	
					$arr_bank_other=implode('>*',$arr_bank_temp1);
					}
					else
					$arr_bank_other=null;
					
				
					$oModelProduct->setData('product_other_payment_method',$arr_bank_other);
				}
			
		
				
			$result = $oModelProduct->do_insert_new_product();
			if ($result == 0) {
				$result = true;
				}
            if (!$result) {
                $sErrorMsg = $oModelProduct->aLanguage['TEXT']['form_cannot_submit'] . $oModelProduct->
                    aLanguage['TEXT']['alert_unique'];
                $oModelProduct->setErrorMsg($sErrorMsg);
            } 
            else {
            	
            		// do update other method transport 
			$aAllDataOther = array();
				for ($i=1; $i<11;$i++)
					{
						if($_REQUEST["other_title_".$i]!='')
						{
							$item = array('other_product_id'=>$result,
											'other_title_of_method'=>$_REQUEST["other_title_".$i],
											'other_link_to_price_list'=>$_REQUEST["other_link_to_price_list_".$i],
											'other_uniform_price'=>$_REQUEST["other_uniform_price_".$i]
											);
							array_push($aAllDataOther,$item);
						}
					}
			$resultUpdateOMoT	= $oModelProduct->updateOMoT($oModelProduct->getData('id'),$aAllDataOther);
                redir('?sMod=product_index&status=insert_success');
            }
			break;
		}
	    case 'do_delete':
        {
            $oModelProduct->setData('product_id', $oModelProduct->getData('id'));
            $result_del = $oModelProduct->deleteProduct();
            $result_del_image = $oModelProduct->deleteImageOfProduct();
            $oModelProduct->deleteProAtt();
			$oModelProduct->deleteOMoT($oModelProduct->getData('id'));
			
            if (!$result_del || !$result_del_image) {
                redir('?sMod=product_index&sSatus=delError');
                $sErrorMsg = $oModelProduct->aLanguage['TEXT']['cannot_delete'] . $oModelProduct->
                    aLanguage['TEXT']['alert_unique'];
                $oModelProduct->setErrorMsg($sErrorMsg);
            } else {
                redir('?sMod=product_index&status=deletesucess');
            }
        }
    case 'edit_form':
        {
        	$oModelProduct->setData("do_edit_or_add_new","do_edit");
            $oModelProduct->setData('product_id', $oModelProduct->getData('id'));
            $oViewProduct->displayEditProductIndex($oModelProduct);
            $rs=$oViewProduct->displayInfoProduct($oModelProduct);
            $oViewProduct->displayListOMoT($oModelProduct,$oModelProduct->getData('id'));
            $oViewProduct->displayListTemplate($oModelProduct,$rs);
            $oViewProduct->display_list_size_edit($oModelProduct,$rs['product_attribute']);
            $oViewProduct->displayRank($oModelProduct,$rs['product_rank_name']);
            $oViewProduct->displayBlockProductBankInputWithData($rs['product_bank']);
            $oViewProduct->displayBlockOtherPayMentMethodWithData($rs['product_other_payment_method']);
            break;
        }
    case 'do_edit':
        {
		$oModelProduct->setData('product_id', $oModelProduct->getData('product_id'));
        $oModelProduct->setData('id', $oModelProduct->getData('product_id'));	
        if (intval($_REQUEST["product_description_normal"]) == 0) {
				$_REQUEST["product_description"]  = $_REQUEST["product_description_html_tag"];
				$oModelProduct->setData("product_description",$_REQUEST["product_description"]);
			}
			$_REQUEST['product_pictures_comment']='';
			for ($i=1;$i<=3;$i++){
				$_REQUEST['product_pictures_comment'] .= $_REQUEST['product_pictures_comment_'.$i]."139211";
			}
        
            $oModelProduct->setData('product_pictures_comment', $_REQUEST['product_pictures_comment']);
           
            $arrayName = array("person","product_send_cash","product_POD","product_credit_card"
            					,"product_limited_assesment_of_the_bidder","product_auto_extend","product_end_soon"
            					,"product_have_to_negotiate_discount","product_title_bold_line","product_bakground_color"
            					,"product_store_hot_auction","product_icon_gift","product_affiliate","product_yahoo_point","product_use_yahoo"
            					,"product_transport_goods_abroad");
            					
            $oModelProduct->setData("product_name",$_REQUEST["product_title"]);
            $oModelProduct->setData("product_yahoo_point_fees",$_REQUEST["product_yahoo_point_fees"]);
      		$_REQUEST_2 = $oModelProduct->setDataForCheckbox($arrayName,$_REQUEST);
      		$_REQUEST_2["product_last_modified"] = date('Y-m-d H:i:s');
   			
   			if($_REQUEST['product_person']=='')
   			{
   				$_REQUEST_2['product_set_limit_on_prices']='';	
   			}
   			/**
			 * BANK && OTHER MOETHOD
			 */
				$product_payment_method='';
				if(is_array($_POST['product_bank']))
				{
					foreach($_POST['product_bank'] as $arr_row)
					{
						if($arr_row!='')
						{	$product_payment_method=1;
							$arr_bank_temp[]=$arr_row;
						}
					}
					if(is_array($arr_bank_temp))
					{	
					$arr_bank=implode('>*',$arr_bank_temp);
					}
					else
					$arr_bank=null;
					$_REQUEST_2['product_bank']=$arr_bank;
				}
			if($product_payment_method==1 && $_POST['product_payment_method']==1)
			{
				$_REQUEST_2['product_payment_method']=1;	
			}
			else
			{
				
				$_REQUEST_2['product_payment_method']=0;
				$_REQUEST_2['product_bank']=null;
			}
			
			//OTHER METHOD
			/**
			 * 
			 */
			 	if(is_array($_POST['product_other_payment_method']))
				{
					foreach($_POST['product_other_payment_method'] as $arr_row1)
					{
						if($arr_row1!='')
						{	
							$arr_bank_temp1[]=$arr_row1;
						}
					}
					if(is_array($arr_bank_temp1))
					{	
					$arr_bank_other=implode('>*',$arr_bank_temp1);
					}
					else
					$arr_bank_other=null;
					
					$_REQUEST_2['product_other_payment_method']=$arr_bank_other;
				}
			 
			 
			$result_edit=$oModelProduct->updateProduct($_REQUEST_2);
			$result_editAttribute=$oModelProduct->updateAttribute($_REQUEST);
			
			// do update other method transport 
			$aAllDataOther = array();
				for ($i=1; $i<11;$i++)
					{
						if($_REQUEST["other_title_".$i]!='')
						{
							$item = array('other_product_id'=>$oModelProduct->getData('id'),
											'other_title_of_method'=>$_REQUEST["other_title_".$i],
											'other_link_to_price_list'=>$_REQUEST["other_link_to_price_list_".$i],
											'other_uniform_price'=>$_REQUEST["other_uniform_price_".$i]
											);
							array_push($aAllDataOther,$item);
						}
					}
			$resultUpdateOMoT	= $oModelProduct->updateOMoT($oModelProduct->getData('id'),$aAllDataOther);
        	//print_r($aAllDataOther); ; die();
			if(!$result_edit)
			{
				redir('?sMod=product_index&sSatus=updateError');
                $sErrorMsg = $oModelProduct->aLanguage['TEXT']['cannot_update'] . $oModelProduct->
                    aLanguage['TEXT']['alert_unique'];
                $oModelProduct->setErrorMsg($sErrorMsg);
			}
			else
			{
				// update comment on pictures table 
		/*		$find_pictures_id = array_keys($_REQUEST);
				$arr_pictures_id = array();
				foreach ($find_pictures_id as $id_key)
						if (is_int($id_key) || is_float($id_key)) 
								array_push($arr_pictures_id,$id_key);
				
				if (count($arr_pictures_id)>0 && is_array($arr_pictures_id))
					$result_edit_2=$oModelProduct->updateComment($_REQUEST,$arr_pictures_id);*/
			redir('?sMod=product_index&sAction=edit_form&status=updateSuccess&id='.$oModelProduct->getData('product_id'));
			}
            break;
        }
        case 'find_product_form':
        {
            $oModelProduct->setData("do_edit_or_add_new","do_edit");
			$oViewProduct->displayFindProductIndex($oModelProduct);
			$oViewProduct->viewListProvince($oModelProduct);
			$oViewProduct->showAttributeAddNew($oModelProduct);
			$oViewProduct->displayRank($oModelProduct,'');
			$oViewProduct->displayBlockProductBankInputDefault($oModelProduct);
        	break;
        }
        
        case "AJAX_GET_INFOR":
		{
			$oViewProduct->displayAddNewForm($oModelProduct);
			$oViewProduct->resultXML_AJAX_GET_INFOR($oModelProduct,$_REQUEST['product_id']);
			die();
			break;
		}	
        
        case 'choose_template':
        {
        	$oViewProduct->displayBlankProductIndex($oModelProduct);
        	$template_path='/template/temp'.$_REQUEST['template_path']."/";
        	$size=$_REQUEST['size'];
        	$oViewProduct->ajax_display_template($oModelProduct,$template_path,$size);
        	die();
        	break;
        }
        
        case 'choose_size':
        {
        	$template_path='/template/temp'.$_REQUEST['template_path']."/";
        	$size_id=$_REQUEST['size_id'];
        	$oViewProduct->ajax_change_size($oModelProduct,$template_path,$size_id);
        	die();
        	break;
        }

}

$oViewProduct->displayPage($oModelProduct);
?>